package com.yuantu.judiciary.dao;

import com.yuantu.judiciary.model.domain.SearchRecordDO;
import com.yuantu.judiciary.model.enums.SearchType;
import com.yuantu.judiciary.model.vo.SearchRecordVO;
import kotlin.jvm.internal.MagicApiIntrinsics;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * 搜索记录数据库操作接口
 *
 * @author syw
 * @since 2023-07-11
 **/
public interface ISearchRecordDao extends JpaRepository<SearchRecordDO, Long> {

    SearchRecordDO findFirstByCreatorIdAndTypeAndContentAndDeletedFalse(Long creatorId, SearchType type, String content);

    List<SearchRecordDO> findFirst10ByCreatorIdAndTypeAndDeletedFalseOrderByCreateTimeDesc(Long creatorId, SearchType type);

    @Modifying
    @Query("update SearchRecordDO set deleted = true, modifierId = :accountId, modifyTime = :currentTime " +
            "where creatorId = :accountId and type = :type and deleted = false ")
    void deleteByCreatorIdAndType(Long accountId, SearchType type, Timestamp currentTime);

    @Query("select a.content as content,count(a.content) as num from SearchRecordDO a " +
            "where a.type = 'ENTERPRISE' and a.deleted = false group by a.content order by num DESC ")
    List<Map<String,Object>> findTop10();
}
